/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import fse from 'fs-extra';
import { resolve } from 'path';

import { BuildDirConst } from '../../src/const/build-directory-const';

import { initByStageEmptyApp, initByStageEmptyService } from './common/common';
import { ProjectBuildProfileModifier } from './common/modifier/project-build-profile-modifier';
import { Probes } from './common/probes';
import { testPreviewForService, testPreviewResource } from './common/task/common-preview';

// 增量方式预览参数
const compileResIncParam = 'compileResInc=true';


describe('test Preview Stage app', () => {
  test.concurrent('stage app entry preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      testPreviewResource(projectDir, 'entry', [compileResIncParam]);
      const probes = new Probes(projectDir, resolve(projectDir, 'entry'), 'default', 'default', '.preview');
      expect(fse.existsSync(resolve(probes.getIntermediatesRes(), BuildDirConst.IDS_MAP))).toBeFalsy();
    });
  });

  test.concurrent('stage app feature preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      testPreviewResource(projectDir, 'feature', [compileResIncParam]);
    });
  });

  test.concurrent('stage app entry api10 preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      testPreviewResource(projectDir, 'entry', [compileResIncParam]);
      const probes = new Probes(projectDir, resolve(projectDir, 'entry'), 'default', 'default', '.preview');
      expect(fse.readdirSync(resolve(probes.getIntermediatesRes(), BuildDirConst.IDS_MAP)).length).toBeGreaterThan(0);
      fse.outputJSONSync(resolve(projectDir, 'entry/src/main/resources/base/element/float.json'), {
        'float': [
          {
            'name': 'float_1',
            'value': '30.6'
          }
        ]
      });
      expect(testPreviewResource(projectDir, 'entry', [compileResIncParam]).stdout).toContain('--defined-ids');
    }, {
      prepare: async (dir) => {
        const projectProfileJsonModifier = new ProjectBuildProfileModifier(dir);
        projectProfileJsonModifier.setRuntimeInfo('HarmonyOS', '4.0.0(10)', '4.0.0(10)').outputSync();
      }
    });
  });

  test.concurrent('stage app library preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      testPreviewResource(projectDir, 'library', [compileResIncParam]);
    });
  });

  test.concurrent('stage app library1 preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      testPreviewResource(projectDir, 'library1', [compileResIncParam]);
    });
  });
});

describe('test Preview Stage service', () => {
  test.concurrent('stage service entry preview', async () => {
    await initByStageEmptyService(async (projectDir) => {
      testPreviewForService(projectDir, 'entry', [compileResIncParam]);
    });
  });

  test.concurrent('stage service feature preview', async () => {
    await initByStageEmptyService(async (projectDir) => {
      testPreviewForService(projectDir, 'feature', [compileResIncParam]);
    });
  });

  test.concurrent('stage service library preview', async () => {
    await initByStageEmptyService(async (projectDir) => {
      testPreviewForService(projectDir, 'library', [compileResIncParam]);
    });
  });

  test.concurrent('stage service library1 preview', async () => {
    await initByStageEmptyService(async (projectDir) => {
      testPreviewForService(projectDir, 'library1', [compileResIncParam]);
    });
  });
});
